import { _decorator, Node, AssetManager, Prefab, instantiate } from 'cc';
import { NewClass } from '../../Base/Global';
import { Singleton } from '../../Base/Singleton';
import { AssetCollectionInfo } from '../ResourceLoad/_c/AssetCollectionInfo';
import { UILayer } from './base/IUILevel';
import { UIBase } from './UIBase';
import { UIManager } from './UIManager';
const { ccclass, property } = _decorator;

export abstract class UIWindow  extends UIBase {
    private uiLayer : UILayer;
    private _collection : AssetCollectionInfo = null;
    public SetUILayer(layer : UILayer)
    {
        this.uiLayer = layer;
    }
    public GetUILayter() : UILayer
    {
        return this.uiLayer;
    }

    public RequestAsset()
    {
        if(this.uiNode != null)
        {
            Singleton.Instance(UIManager).UIAssetLoadDoneCallBack(this);
            return;
        }

        // 增加到下载列表中
        var assetInfo = this.GetAssetInfo();
        if(assetInfo == null){
            console.error("can not find assetInfo ! " + this.className);
            return;
        }
        var collection = NewClass(AssetCollectionInfo);
        collection.AddAssetInfo(assetInfo);

        // 等到下载列表完成以后进行初始化
        var preloadAssetInfos = this.GetPreloadAssetInfos();
        if(preloadAssetInfos != null)
        {
            preloadAssetInfos.forEach((info)=>{
                collection.AddAssetInfo(info);
            });
        }
        var instanceId = this.instanceId;
        collection.AddLoadDoneCallBack((data : any, colection : AssetCollectionInfo)=>{
            var className = data as string;
            var oUI = Singleton.Instance(UIManager).GetOpenUIByClassName(className);
            if(oUI != null && oUI.instanceId == instanceId)
            {
                oUI.SetAssetCollection(colection);
                var assetInfo = oUI.GetAssetInfo();
                var bundle = oUI.GetAssetBundle(assetInfo);
                if(bundle != null)
                {
                    bundle.load<Prefab>(assetInfo.assetName, (err, asset)=>
                    {
                        var _oUI = Singleton.Instance(UIManager).GetOpenUIByClassName(className);
                        if(_oUI != null && oUI.instanceId == instanceId)
                        {
                            _oUI.Init(instantiate(asset) as Node);
                            Singleton.Instance(UIManager).UIAssetLoadDoneCallBack(_oUI);
                        }
                        else
                        {
                            asset.destroy();
                        }
                    });
                }
                else
                {
                    console.error("can not find bundle ! bundleName: " + assetInfo.assetBundleName + "  className:" + oUI.className);
                    Singleton.Instance(UIManager).UIAssetLoadDoneCallBack(oUI);
                }
            }
            else
            {
                collection.Release();
            }

        }, this.className);
        collection.StartRequestAsset();
    }

    public SetAssetCollection(collection : AssetCollectionInfo)
    {
        super.SetAssetCollection(collection);
        this._collection = collection;
    }
    public RecycleAsset()
    {
        super.RecycleAsset();
        if(this._collection != null)
        {
            return this._collection.Release();
        }   
    }
    
    // 便捷接口。 实际上还是调用UIManager.Instance.DestroyUI 方法
    public OnDestroySelf(){Singleton.Instance(UIManager).DestroyUI(this);}
}